import router from 'umi/router';
import { message } from 'antd';
import { getCurrentQps,getAllDevices,getAllQps } from './services';

export default {
  namespace: 'qps',
  state: {
    curData: 0,
    allDevices: []
  },

  effects: {

    *getAllDevices({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getAllDevices, payload);
        if(res.data.length>0) {
          callback(res.data[0]);
        }
        yield put({
          type: 'save',
          payload: {
            allDevices: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      }
    },

    *getCurrentQps({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getCurrentQps, payload);
        callback(res.data.qps);
      } catch (e) {
        callback(e);
        message.error(e.message);
      }
    },

    *getAllQps({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getAllQps, payload);
        callback(res.data)
      } catch (e) {
        message.error(e.message);
      }
    },
  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
